package com.app.teaware.goods;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.app.teaware.R;
import com.app.teaware.basis.BasisActivity;
import com.app.teaware.basis.Constants;
import com.app.teaware.bean.GoodsListBean;
import com.app.teaware.bean.GoodsListData;
import com.app.teaware.bean.LoginBean;
import com.app.teaware.util.MyUtils;
import com.app.teaware.util.OnClickUtils;
import com.jaredrummler.materialspinner.MaterialSpinner;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import my.http.MyHttpListener;
import my.http.MyHttpUtils;
import my.http.MyParams;

/**
 * 领料管理-货物资料
 */
public class GoodsDataActivity extends BasisActivity {

    @BindView(R.id.recyclerview)
    RecyclerView mRecyclerView;
    private MyAdapterList mAdapter;
    private GoodsListBean mListBean;
    @BindView(R.id.spinner)
    MaterialSpinner spinner;
    String spinSid;
    @BindView(R.id.edit_gcd)
    EditText edit_gcd;
    @BindView(R.id.edit_gna)
    EditText edit_gna;


    @Override
    public void initViews() {
        setContentView(R.layout.goods_data_activity);
        setTitle("货品资料");
        setTitleLeftButton(null);
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        mListBean = new GoodsListBean();
        setListView();
        setTitleLeftButton(null);
        initSpiner();
    }


    private void  initSpiner(){
        showLoading();
        MyParams params = new MyParams();
        params.put("aid",LoginBean.getInstance().getAcode() );
        MyHttpUtils.get(Constants.URL_SPECIES_SELECT_ALL, params, new MyHttpListener() {
                    @Override
                    public void onSuccess(int httpWhat, Object result) {
                        if (isFinishing()) return;
                        GoodsListBean listBean = (GoodsListBean) result;
                        if(listBean != null && listBean.getList().size()>0){
                            spinSid = listBean.getList().get(0).getSid();
                            spinner.setItems(listBean.getAllNameList());
                            spinner.setOnItemSelectedListener(new MaterialSpinner.OnItemSelectedListener<String>() {
                                @Override public void onItemSelected(MaterialSpinner view, int position, long id, String item) {
                                    GoodsListData data = listBean.getCompanyByChoose(position);
                                    spinSid = data.getSid();
                                    getList();
                                }
                            });
                        }
                        getList();
                    }

                    @Override
                    public void onFailure(int httpWhat, Object result) {
                        super.onFailure(httpWhat, result);
                        dismissProgress();
                    }

                    @Override
                    public void onFinish(int httpWhat) {

                    }
                },
                0, GoodsListBean.class);
    }

    private void setListView() {
        if (mAdapter == null) {
            mAdapter = new MyAdapterList();
            mRecyclerView.setAdapter(mAdapter);
        } else {
            mAdapter.notifyDataSetChanged();
        }
        setEmptyView();
    }

    private void setEmptyView() {
        if (mListBean == null || mListBean.getList().size() == 0) {
            findViewById(R.id.refresh_view).setVisibility(View.VISIBLE);
        } else {
            findViewById(R.id.refresh_view).setVisibility(View.GONE);
        }
    }

    @OnClick(R.id.img_search)
    public void search(){
        getList();
    }

    private void getList() {
        mListBean.getList().clear();
        if(TextUtils.isEmpty(spinSid)){
            showToast("无货品资料");
            return;
        }

        MyParams params = new MyParams();
        showProgress();
        params.put("aid",LoginBean.getInstance().getAcode());
        params.put("sid", spinSid);
        if(!TextUtils.isEmpty(edit_gna.getText().toString())){
            params.put("gna", edit_gna.getText().toString().trim());
        }
        if(!TextUtils.isEmpty(edit_gcd.getText().toString())){
            params.put("gcd", edit_gcd.getText().toString().trim());
        }

        MyHttpUtils.get(Constants.URL_GOODS_SELECT_LISTS, params, new MyHttpListener() {
                    @Override
                    public void onSuccess(int httpWhat, Object result) {
                        if (isFinishing()) return;
                         mListBean  = (GoodsListBean) result;
                    }

                    @Override
                    public void onFailure(int httpWhat, Object result) {
                        super.onFailure(httpWhat, result);
                    }

                    @Override
                    public void onFinish(int httpWhat) {
                        dismissProgress();
                        setListView();
                    }
                },
                0, GoodsListBean.class);

    }


    public class MyAdapterList extends RecyclerView.Adapter<MyAdapterList.ViewHolder> {

        public MyAdapterList() {

        }

        //创建新View，被LayoutManager所调用
        @Override
        public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.goods_data_list_item, viewGroup, false);
            return new ViewHolder(view);
        }

        //将数据与界面进行绑定的操作
        @Override
        public void onBindViewHolder(ViewHolder viewHolder, int position) {
            viewHolder.bind(position);
        }

        //获取数据的数量
        @Override
        public int getItemCount() {
            return mListBean == null ? 0 : mListBean.getList().size();
        }

        //自定义的ViewHolder，持有每个Item的的所有界面元素
        public class ViewHolder extends RecyclerView.ViewHolder {
            @BindView(R.id.list_item)
            public View list_item;
            @BindView(R.id.text_name)
            public TextView text_name;
            @BindView(R.id.text_specs)
            public TextView text_specs;
            @BindView(R.id.text_unit)
            public TextView text_unit;
            @BindView(R.id.text_gcd)
            public TextView text_gcd;
            @BindView(R.id.text_gno)
            public TextView text_gno;
            @BindView(R.id.btn_ok)
            public TextView btn_ok;
//            @BindView(R.id.img_icon)
//            public ImageView img_icon;


            public ViewHolder(View view) {
                super(view);
                ButterKnife.bind(this, view);
//                img_icon.setVisibility(View.VISIBLE);
            }

            public void bind(int position) {
                GoodsListData data = mListBean.getList().get(position);
//                img_icon.setImageResource(R.drawable.icon_doc);
                MyUtils.setNotNullText(text_name,data.getGna());
                MyUtils.setNotNullText(text_specs,data.getGsp());
                MyUtils.setNotNullText(text_unit,data.getGun());
                MyUtils.setNotNullText(text_gcd,data.getGcd());
                MyUtils.setNotNullText(text_gno,data.getGno());
                btn_ok.setTag(data);
                list_item.setTag(data);

            }
            @OnClick(R.id.btn_ok)
            void onItemClick(View view){
                if (!OnClickUtils.isFastClick()) {
                    GoodsListData data = (GoodsListData) view.getTag();
                    Intent intentResult = new Intent();
                    intentResult.putExtra("data",data);
                    setResult(Constants.GOODSDATARESULT,intentResult);
                    finish();

                }
            }
        }
    }

}
